'use strict';

const Controller = require('egg').Controller;
const crypto = require('crypto');
class HomeController extends Controller {
  /**
   * 主页渲染
   * 
   * @memberof HomeController
   */
  async index() {
        const { ctx, service } = this;
        let user = ctx.session.user;
        let lists=await service.index.projectInfoModel();

        await ctx.render('home/home.html', {
            user,
            lists
        })
    }
    /**
     * 登录
     * 
     * @memberof HomeController
     */
    async login() {
        const { ctx, service } = this;
        let { account, password } = ctx.request.body;
        let pwd=crypto.createHash('sha256',password).update('nodeJS').digest('hex');
        let result = await service.home.home.login({account, password:pwd});
        if (result !== null) {
            ctx.session.user = result;
            ctx.body = {
                success: true,
                msg: '登录成功'
            }
        } else {
            ctx.body = {
                success: false,
                msg: '登录失败,账号或者密码错误！'
            }
        }
    }
    /**
     * 退出登录
     * 
     * @memberof HomeController
     */
    async loginout() {
        const { ctx, service } = this;
        ctx.session = null;
        ctx.body = {
            success: true,
            msg: '退出成功'
        }
    }
    //用户注册
    async register() {
        const { ctx, service } = this;
        let { account, password, type } = ctx.request.body;
        let pwd=crypto.createHash('sha256',password).update('nodeJS').digest('hex');
        let result = await service.home.home.register({ account, password:pwd, type })
        if (result.result > 0) {
            ctx.body = { success: true, msg: '添加成功!' }
        } else {
            ctx.body = { success: false, msg: '添加失败!' }
        }
    }

}

module.exports = HomeController;